package com.fubowen.reader.extension

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.fubowen.reader.adapter.AutoGridLayoutManager

fun LayoutManager.getItemView(recycler: RecyclerView.Recycler, position: Int, isBottom: Boolean = true): ItemView {
    if (isBottom) {
        return getItemView(recycler, position, -1)
    }
    return getItemView(recycler, position, 0)
}

fun LayoutManager.getItemView(recycler: RecyclerView.Recycler, position: Int, index: Int): ItemView {
    val view = recycler.getViewForPosition(position)
    addView(view, index)
    measureChildWithMargins(view, 0, 0)
    val w = getDecoratedMeasuredWidth(view)
    val h = getDecoratedMeasuredHeight(view)
    return ItemView(view, h, w)
}

data class ItemView(
    var view: View,
    var height: Int,
    var width: Int
)

data class AppendResult(
    var position: Int,
    var lineHeight: Int
)